//
//  AppDelegate.m
//  GameSDK
//
//  Created by Johnny on 2017/7/25.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "AppDelegate.h"
#import "GSDKComplatformBase.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    
    [self initNotification];
    
    [self.window makeKeyAndVisible];
    
    GSDKINITCONFIGURE.gid = @"100003";
    GSDKINITCONFIGURE.appKey = @"d035ab136f69476e7114980dc1a2d281";
    GSDKINITCONFIGURE.version = @"1.0.0";
    GSDKINITCONFIGURE.versionName = @"1.0.0";
    [GSDKINITCONFIGURE setSDKLanguage:GSDK_LANGUAGE_EN];
    [GSDKINITCONFIGURE showLoadingView];
    [GSDKINITCONFIGURE application:application didFinishLaunchingWithOptions:launchOptions];
    
    return YES;
}

- (void)initNotification {
    //获取更新回调
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(checkUpdateResult:)
                                                 name:GSDKCheckGameUpdateFinishNotification
                                               object:nil];
    //初始化成功回调
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(initResult:)
                                                 name:GSDKInitDidFinishNotification
                                               object:nil];
    //用户登录注册操作回调
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userResult:)
                                                 name:GSDKUserDidFinishNotification
                                               object:nil];
    //进入平台操作回调
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(toolBarResult:)
                                                 name:GSDKToolBarDidFinishNotification
                                               object:nil];
    //内购操作回调
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(iapResult:)
                                                 name:GSDKIAPDidFinishNotification
                                               object:nil];
}

- (void)initResult:(NSNotification *)noti {
    [GSDKCOMPLATFORM login];
}

- (void)userResult:(NSNotification *)noti {
    NSDictionary * dic  = noti.userInfo;
    GSDKAccountStatus * aStatus = dic[@"GSDKAccountStatus"];
    GSDKMessageStatus * mStatus = dic[@"GSDKMessageStatus"];
    NSLog(@"userResult msg = %@", mStatus.msg);
    if (aStatus.gSDKAccountStatus == GSDK_ACCOUNT_STATUS_MEMBER) {
        if ([mStatus.code isEqualToString:@"0"]) {
            [GSDKCOMPLATFORM showSuspensionToolBar];
        }
    }
    if (aStatus.gSDKAccountStatus == GSDK_ACCOUNT_STATUS_LOGOUT) {
        if ([mStatus.code isEqualToString:@"0"]) {
            [GSDKCOMPLATFORM hideSuspensionToolBar];
        }
    }
}

- (void)checkUpdateResult:(NSNotification *)noti {
    NSDictionary * dic = noti.userInfo;
    GSDKVersionStatus * vStatus = dic[@"GSDKVersionStatus"];
    GSDKMessageStatus * mStatus = dic[@"GSDKMessageStatus"];
    if ([mStatus.code isEqualToString:@"0"]) {
        NSLog(@"有更新 %@", vStatus.downLoadUrl);
    }
}

- (void)iapResult:(NSNotification *)noti {
    NSDictionary * dic = noti.userInfo;
    GSDKIAPStatus * vStatus = dic[@"GSDKIAPStatus"];
    GSDKMessageStatus * mStatus = dic[@"GSDKMessageStatus"];
    if ([mStatus.code isEqualToString:@"0"]) {
        if (vStatus.gSDKIAPStatus == GSDK_IAP_STATUS_PRODUCTLIST) {
            [self.viewController reloadAt:dic[@"goods"]];
        } else {
            NSLog(@"%@", mStatus.msg);
        }
    } else {
        NSLog(@"%@", mStatus.msg);
    }
}

- (void)toolBarResult:(NSNotification *)noti {
    BOOL isOpen = [noti.object boolValue];
    if (isOpen) {
        NSLog(@"进入平台界面");
    } else {
        NSLog(@"退出平台界面");
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [GSDKCOMPLATFORM application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}


@end
